package com.mobiletrain.org.motherkitch.fenlei;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.mobiletrain.org.motherkitch.R;

import java.util.ArrayList;

import com.mobiletrain.org.motherkitch.fenlei.bean.ListItem;

public class FenleiFragment extends Fragment {


    private ListView list;
    private ArrayList<ListItem> lists;
    private static final String ARG_PARAM1 = "param1";
    private static final String ARG_PARAM2 = "param2";

    private String mParam1;
    private String mParam2;

    public static FenleiFragment newInstance(String param1,String param2) {
        FenleiFragment fragment = new FenleiFragment();
        Bundle args = new Bundle();
        args.putString(ARG_PARAM1, param1);
        args.putString(ARG_PARAM2, param2);
        fragment.setArguments(args);
        return fragment;
    }
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View view = inflater.inflate(R.layout.fragment_fenlei, container, false);
        list= (ListView) view.findViewById(R.id.list);



        lists = new ArrayList<ListItem>();
        int a[]={R.drawable.ca_icon1,R.drawable.ca_icon2,
                R.drawable.ca_icon3,R.drawable.ca_icon4,R.drawable.ca_icon5
                ,R.drawable.ca_icon6,R.drawable.ca_icon7,R.drawable.ca_icon8};
        final String b[]={"备孕","孕早期","孕中期","孕晚期","月子餐","2—6个月","6—12个月","1岁以上"};
        for (int i = 0; i <8 ; i++) {
            lists.add(new ListItem(a[i], b[i]));
        }
        list.setAdapter(new MyAdapter());
        list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Intent i=new Intent(getActivity(), AfragmentSecondActivity.class);
                Bundle bundle=new Bundle();
                bundle.putInt("id",position+1);
                bundle.putString("name",b[position]);
                i.putExtra("key",bundle);
                startActivity(i);
            }
        });
        return view;
    }

    class MyAdapter extends BaseAdapter{

        @Override
        public int getCount() {
            if(lists!=null){
                return  lists.size();
            }
            return 0;
        }

        @Override
        public Object getItem(int position) {
            return lists.get(position);
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            MyHoder hoder;
            if(convertView==null){
                convertView=getActivity().getLayoutInflater().inflate(R.layout.afragment_list_item, null);
                hoder=new MyHoder();
                hoder.icon= (ImageView) convertView.findViewById(R.id.item_icon);
                hoder.text= (TextView) convertView.findViewById(R.id.item_text);
                convertView.setTag(hoder);
            }else{
                hoder= (MyHoder) convertView.getTag();
            }
            ListItem item = lists.get(position);
            hoder.icon.setImageResource(item.getIcon());
            hoder.text.setText(item.getText());
            return convertView;
        }
        class MyHoder{
            ImageView icon;
            TextView text;
        }
    }

}
